package fun.xinnix.config;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {
    @Bean
    public MqttClient mqttClient() throws MqttException {
        String broker = "tcp://localhost:1883";
        String clientId = "spring01";
        MqttClient client = new MqttClient(broker, clientId, new MemoryPersistence());
        // 连接参数
        MqttConnectOptions options = new MqttConnectOptions();
        // 设置用户名和密码
//        options.setUserName(username);
//        options.setPassword(password.toCharArray());
        options.setConnectionTimeout(60);
        options.setKeepAliveInterval(60);
        // 连接
        client.connect(options);
        if (!client.isConnected()) {
            client.reconnect();
        }
        return client;
    }
}
